//
//  CommonUtils.m
//  YSSmartHome
//
//  Created by yanghao on 2017/3/28.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "CommonUtils.h"

@implementation CommonUtils
+(BOOL)isNewUpdateWithKey:(NSString*)key
{
    NSString* currentVersion =  [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    // 2. 获取沙盒版本信息
    NSString* sandboxVersion = [[NSUserDefaults standardUserDefaults] stringForKey:key];
    // 3. 将版本写入沙盒
    [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
    [[NSUserDefaults standardUserDefaults]synchronize];
    return ![currentVersion isEqualToString:sandboxVersion];
}

+ (int)evaluateColor:(int) startValue endValue:(int)endValue  fraction:(float) fraction {
    if (fraction <= 0) {
        return startValue;
    }
    if (fraction >= 1) {
        return endValue;
    }
    int startInt = startValue;
    int startA = (startInt >> 24) & 0xff;
    int startR = (startInt >> 16) & 0xff;
    int startG = (startInt >> 8) & 0xff;
    int startB = startInt & 0xff;
    
    int endInt = endValue;
    int endA = (endInt >> 24) & 0xff;
    int endR = (endInt >> 16) & 0xff;
    int endG = (endInt >> 8) & 0xff;
    int endB = endInt & 0xff;
    
    return ((startA + (int) (fraction * (endA - startA))) << 24) | ((startR + (int) (fraction * (endR - startR))) << 16) | ((startG + (int) (fraction * (endG - startG))) << 8) | ((startB + (int) (fraction * (endB - startB))));
}


+ (BOOL)dateIsBetweenPeriodOfDate:(NSDate*)date datePeriod:(DatePeriod)datePeriod
{
    NSDate* currentDate = [NSDate date];
    NSDateFormatter*outputFormatter = [[NSDateFormatter alloc]init];
    outputFormatter.dateFormat = @"yyyyMMdd";
    NSString* string =  [outputFormatter stringFromDate:currentDate];
    
    NSString* beginDataStr =  nil;
    NSString* endDateStr = nil;
    switch (datePeriod) {
        case DatePeriod03:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@000000",string];
            endDateStr =  [NSString stringWithFormat:@"%@025959",string];
        }
            break;
        case DatePeriod06:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@030000",string];
            endDateStr =  [NSString stringWithFormat:@"%@055959",string];
        }
            break;
        case DatePeriod09:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@060000",string];
            endDateStr =  [NSString stringWithFormat:@"%@085959",string];
        }
            break;
        case DatePeriod12:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@090000",string];
            endDateStr =  [NSString stringWithFormat:@"%@115959",string];
        }
            break;
        case DatePeriod15:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@120000",string];
            endDateStr =  [NSString stringWithFormat:@"%@145959",string];
        }
            break;
        case DatePeriod18:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@150000",string];
            endDateStr =  [NSString stringWithFormat:@"%@175959",string];
        }
            break;
        case DatePeriod21:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@180000",string];
            endDateStr =  [NSString stringWithFormat:@"%@205959",string];
        }
            break;
        case DatePeriod24:
        {
            beginDataStr =  [NSString stringWithFormat:@"%@210000",string];
            endDateStr =  [NSString stringWithFormat:@"%@235959",string];
        }
            break;
        default:
            break;
    }
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    //    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] ];
    [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate* beginData = [inputFormatter dateFromString:beginDataStr];
    NSDate* endDate = [inputFormatter dateFromString:endDateStr];
    
    BOOL result  = [self date:date isBetweenDate:beginData andDate:endDate];
    if (result) {
        NSLog(@"在时间段");
    }
    else
    {
        NSLog(@"不在时间段");
    }
    return result;
}

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] ==NSOrderedAscending)
        return NO;
    
    if ([date compare:endDate] ==NSOrderedDescending)
        return NO;
    
    return YES;
}
@end
